﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.Helper;
using Ext.Net;

namespace ExaminationSys.View.Section
{
    public partial class SectionSelectPaper : System.Web.UI.Page
    {

        private static readonly SectionPaperService Proxy = ObjectFactory<SectionPaperService>.Create();
    

        /// <summary>
        /// 栏目类型
        /// </summary>
        public string SectionID
        {
            get { return Request.QueryString["SectionID"] == null ? string.Empty : Request.QueryString["SectionID"].ToString(); }
        }

        /// <summary>
        /// 测试类型
        /// </summary>
        public int EnumTestType
        {
            get
            {
                if (Request.QueryString["EnumTestType"] != null)
                {
                    try
                    {
                        return Convert.ToInt32(Request.QueryString["EnumTestType"].ToString());
                    }
                    catch (Exception e) { return -1; };
                }
                return -1;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!X.IsAjaxRequest)
            {
                NavLabel.Text = EnumTestType == 1 ? NavLabel.Text + "动机测试" : NavLabel.Text + "水平测试";
                SectionPaper entity = null;
                try
                {
                    entity = Proxy.Get(new Guid(SectionID), EnumTestType);
                }
                catch
                {
                    Response.Redirect("SectionDetailList.aspx");
                }

                if(entity!=null)
                {
                    PaperID.Text = entity.PaperName;
                    HidPaperId.Value = entity.PaperID.ToString();
                
                }

                HidSectionID.Value = SectionID;
                HidTestType.Value = EnumTestType.ToString();



            }

        }

        /// <summary>
        /// 保存sectionPaper
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SaveSectionPaper(object sender,DirectEventArgs e)
        {

            SectionPaper entity = null;
            try
            {
                entity = Proxy.Get(new Guid(SectionID), EnumTestType);
            }
            catch
            {
                Response.Redirect("SectionDetailList.aspx");
            }

            if(PaperID.Text.Trim()==string.Empty)
            {
                MessageBoxHelper.ShowWarning("试卷不能为空，请选择");
                return;
            }

            //新增
            if (entity == null)   
            {
                entity = new SectionPaper();
                entity.PaperID = new Guid(this.HidPaperId.Value);
                entity.PaperName = this.PaperID.Text;


                var op = FormsAuthenticationHelper.GetCurrentUserID();
                if (string.IsNullOrEmpty(op))
                {
                    FormsAuthenticationHelper.SignOut();
                    Response.Redirect("~/Account/Login.aspx");
                }
                entity.Opeator = op;

                entity.OpeatorTime = DateTime.Now;
                entity.Status = 1;
                entity.SectionInfoID = new Guid(SectionID);
                entity.TestType = int.Parse(HidTestType.Value);

                Proxy.Insert(entity);

                MessageBoxHelper.ShowWarning("保存成功!");

                                
            }
            else //更新
            {
  
                entity.PaperID = new Guid(this.HidPaperId.Value);
                entity.PaperName = this.PaperID.Text;

                var op = FormsAuthenticationHelper.GetCurrentUserID();
                if (string.IsNullOrEmpty(op))
                {
                    FormsAuthenticationHelper.SignOut();
                    Response.Redirect("~/Account/Login.aspx");
                }
                entity.Opeator = op;

                entity.OpeatorTime = DateTime.Now;
                entity.Status = 1;
                entity.SectionInfoID = new Guid(SectionID);
                entity.TestType = int.Parse(HidTestType.Value);

                Proxy.Update(entity);
                MessageBoxHelper.ShowWarning("保存成功!");

                
            }
        }

        /// <summary>
        /// 保存sectionPaper
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CancelSectionPaper(object sender, DirectEventArgs e)
        {
            Response.Redirect("SectionDetailList.aspx");
        }
    }
}